package cn.hengzhu.main_manager.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.hengzhu.main_manager.repository.dao.ErrorLogRecordMapper;
import cn.hengzhu.main_manager.repository.domain.ErrorLogRecord;
import cn.hengzhu.main_manager.repository.param.PageBaseParam;
import cn.hengzhu.main_manager.utils.AjaxResult;
import cn.hengzhu.main_manager.utils.LayUiResultJson;
import cn.hengzhu.main_manager.utils.ann.MoblieUrl;
import cn.hengzhu.main_manager.utils.ann.SystemControllerLog;

/**
 * 日志管理
 */
@Controller
@RequestMapping("/errLog")
public class ErrorLogController {
	
	@Autowired
	private ErrorLogRecordMapper errorLogRecordMapper;
	@Autowired
	private HttpServletRequest request;
	
	@MoblieUrl
	@RequestMapping("/index")
	public String index(){
		return request.getAttribute("moblie")+"errlog";
	}
	
	@MoblieUrl
	@RequestMapping("/errDeil")
	public String errDeil(Model model,Integer errorId){
		
		ErrorLogRecord selectByPrimaryKey = errorLogRecordMapper.selectByPrimaryKey(errorId);
		String errorMsg = selectByPrimaryKey.getErrorMsg();
		model.addAttribute("errorMsg",errorMsg);
		return request.getAttribute("moblie")+"errDeil";
	}
	
	@MoblieUrl
	@RequestMapping("/error")
	public String error(){
		return request.getAttribute("moblie")+"404";
	}
	
	/**
	 * @author 刘成
	 * @since 2018年7月13日
	 * @param param
	 * @return
	 * 列表
	 */
	@RequestMapping("/getErrorList")
	@ResponseBody
	public LayUiResultJson<ErrorLogRecord> getErrorList(PageBaseParam param){
		PageHelper.startPage(param.getPage(), param.getLimit(),true);
		List<ErrorLogRecord> list=errorLogRecordMapper.getAllList();
		PageInfo<ErrorLogRecord> pageInfo =new PageInfo<>(list);
		long count = pageInfo.getTotal();
		LayUiResultJson<ErrorLogRecord> result = new LayUiResultJson<>(count, list);
		return result;
	}
	
	
	/**
	 * @author 刘成
	 * @since 2018年7月13日
	 * @param errorId
	 * @return
	 * 错误信息详情
	 */
	@RequestMapping("/errorDetails")
	@ResponseBody
	public  Map<String,String>  errorDetails(Integer errorId){
		Map<String,String> map=new HashMap<String, String>();
		
		ErrorLogRecord selectByPrimaryKey = errorLogRecordMapper.selectByPrimaryKey(errorId);
		String errorMsg = selectByPrimaryKey.getErrorMsg();
		map.put("errorMsg", errorMsg);
		return map;
	}
	
	
	
	/**
	 * @author 刘成
	 * @since 2018年7月13日
	 * @param errorId
	 * @return
	 * 删除已解决的问题
	 */
	@SystemControllerLog(description="删除错误日志")
	@RequestMapping("/deleteMsg")
	@ResponseBody
	public  AjaxResult  deleteMsg(Integer errorId){
		try {
			errorLogRecordMapper.deleteByPrimaryKey(errorId);
			return new AjaxResult();
		} catch (Exception e) {
			return new AjaxResult("删除失败");
		}
	}
}
